/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.senaclem.persistencia;

import co.edu.senaclem.modelo.Asignafichas;
import co.edu.senaclem.modelo.Fichatitulacion;
import co.edu.senaclem.modelo.Instructor;
import java.sql.ResultSet;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author DILOVE
 */
@Stateless
public class AsignaFichasDAO implements IAsignaFichasDAO {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public void registrar(Asignafichas asignafichas) throws Exception {
        try {
            entityManager.persist(asignafichas);
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public int generarCodigo() throws Exception {
        try {
            String jpql = "Select max(a.codigoasigna) as maximo From Asignafichas a";
            Query query = entityManager.createQuery(jpql);
            int max = Integer.parseInt(query.getSingleResult().toString());
            return max + 1;
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public int consultarCodigo(Long documentoInstructor, Integer fichaTitulacion) throws Exception {
        try {
            int max = 0;
            String jpql = "Select a.codigoasigna as maximo From Asignafichas a where a.documentoinstructor='" + documentoInstructor + "' and a.fichatitulacion='" + fichaTitulacion + "'";
            Query query = entityManager.createQuery(jpql);
            max = Integer.parseInt(query.getSingleResult().toString());
            return max;
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public Asignafichas consultarPorId(int codigoAsigna) throws Exception {
        try {
            return entityManager.find(Asignafichas.class, codigoAsigna);
        } catch (RuntimeException e) {
            throw e;
        }
    }
}
